home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / readfile.dos < prev    next >
Text File  |  1995-11-22  |  1KB  |  51 lines

  1. /*----------------------------------------------------------------------
  2.     Read whole file into memory
  3.  
  4.   Args: filename -- path name of file to read
  5.  
  6.   Result: Returns pointer to malloced memory with the contents of the file
  7.           or NULL
  8.  
  9. This won't work very well if the file has NULLs in it and is mostly
  10. intended for fairly small text files.
  11.  ----*/
  12. char *
  13. read_file(filename)
  14.     char *filename;
  15. {
  16.     int         fd;
  17.     struct stat statbuf;
  18.     char       *buf;
  19.     int         nb;
  20.  
  21.     fd = open(filename, O_RDONLY|O_TEXT);
  22.     if(fd < 0){
  23.     if(errno != ENOENT){
  24.         sprintf(tmp_20k_buf,
  25.             "Weird error opening \"%s\"\nError: %s (%d)\n",
  26.             error_description(errno), errno);
  27.         fatal(tmp_20k_buf);
  28.     }
  29.     else
  30.       return((char *)NULL);
  31.     }
  32.  
  33.     fstat(fd, &statbuf);
  34.  
  35.     buf = fs_get((size_t)statbuf.st_size + 1);
  36.  
  37.     /*
  38.      * On some systems might have to loop here, if one read isn't guaranteed
  39.      * to get the whole thing.
  40.      */
  41.     if((nb = read(fd, buf, (unsigned)statbuf.st_size)) < 0)
  42.       fs_give((void **)&buf);        /* NULL's buf */
  43.     else
  44.       buf[nb] = '\0';
  45.  
  46.     close(fd);
  47.     return(buf);
  48. }
  49.  
  50.  
  51.